package monster.flight.admin.manager.imp.xtgl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import monster.comm.exception.InvalidParameterException;
import monster.comm.key.Key;
import monster.comm.util.MyBatisBatchInsertSupport;
import monster.comm.util.Tool;
import monster.flight.admin.manager.service.xtgl.RoleService;
import monster.flight.admin.querydao.xtgl.Role_Menu_Tree_queryDAO;
import monster.flight.admin.querydao.xtgl.Role_queryDAO;
import monster.flight.admin.vo.xtgl.Role_Meun_TreeVO;
import monster.flight.comm.entity.dao.Xt_menDAO;
import monster.flight.comm.entity.dao.Xt_roleDAO;
import monster.flight.comm.entity.dao.Xt_rolmenDAO;
import monster.flight.comm.entity.vo.Xt_men;
import monster.flight.comm.entity.vo.Xt_role;
import monster.flight.comm.entity.vo.Xt_rolmen;
import monster.flight.comm.util.KeyConstant;

/**
 * 创建信息============= 类名: monster.flight.admin.manager.service.basic.RoleServiceImp
 * 类作用描述: 角色菜单服务类 创建人： wanghong 创建时间： 2013-1-11 下午02:29:43 修改信息1=============
 * 修改类型: //1.修改bug 2.业务变化修改 修改描述: 修改人： 修改时间：
 */

public class RoleServiceImp implements RoleService {
	private Xt_roleDAO xt_roleDAO;
	private Xt_rolmenDAO xt_rolmenDAO;
	private Xt_menDAO xt_menDAO;
	private Role_queryDAO role_queryDAO;
	private Role_Menu_Tree_queryDAO role_Menu_Tree_queryDAO;
	private MyBatisBatchInsertSupport insertSupport;
	public Role_Menu_Tree_queryDAO getRole_Menu_Tree_queryDAO() {
		return role_Menu_Tree_queryDAO;
	}

	public void setRole_Menu_Tree_queryDAO(
			Role_Menu_Tree_queryDAO role_Menu_Tree_queryDAO) {
		this.role_Menu_Tree_queryDAO = role_Menu_Tree_queryDAO;
	}
	public MyBatisBatchInsertSupport getInsertSupport() {
		return insertSupport;
	}

	public void setInsertSupport(MyBatisBatchInsertSupport insertSupport) {
		this.insertSupport = insertSupport;
	}
    
	public Xt_menDAO getXt_menDAO() {
		return xt_menDAO;
	}

	public void setXt_menDAO(Xt_menDAO xt_menDAO) {
		this.xt_menDAO = xt_menDAO;
	}

	public Xt_roleDAO getXt_roleDAO() {
		return xt_roleDAO;
	}

	public void setXt_roleDAO(Xt_roleDAO xt_roleDAO) {
		this.xt_roleDAO = xt_roleDAO;
	}

	public Xt_rolmenDAO getXt_rolmenDAO() {
		return xt_rolmenDAO;
	}

	public void setXt_rolmenDAO(Xt_rolmenDAO xt_rolmenDAO) {
		this.xt_rolmenDAO = xt_rolmenDAO;
	}

	public Role_queryDAO getRole_queryDAO() {
		return role_queryDAO;
	}

	public void setRole_queryDAO(Role_queryDAO role_queryDAO) {
		this.role_queryDAO = role_queryDAO;
	}

	@Override
	public void insert(Xt_role xt_role) throws Exception {
		Tool.NullException(xt_role, "xt_role 不能为空");
		Tool.NullException(xt_role.getRole_code(), "role_code 不能为空");
		if(checkRoleCode(xt_role.getRole_code())){
			Tool.UniqueException("角色已经存在");
		}
		xt_roleDAO.insert(xt_role);
	}

	@Override
	public void update(Xt_role xt_role) throws Exception {
		Tool.NullException(xt_role, "xt_role 不能为空");
		Tool.NullException(xt_role.getRole_code(), "role_code 不能为空");
		xt_roleDAO.update(xt_role);

	}

	@Override
	public void deleteRoleAndRoleMen(String role_code) throws Exception {
		Tool.NullException(role_code, "xt_role 不能为空");
		xt_rolmenDAO.deleteByRoleCode(role_code);
		xt_roleDAO.delete(role_code);

	}

	@Override
	public List<Xt_role> selectPage(Map<String, Object> map)
			throws Exception {
		Tool.NullException(map, "map 不能为空");
		Tool.NullException(map.get("condition"), "map.condition 不能为空");
		Tool.NullException(map.get("order"), "map.order 不能为空");
		List<Xt_role> list = role_queryDAO.selectPage(map);
		if (list == null) {
			list = new ArrayList<Xt_role>();
		}
		return list;

	}

	@Override
	public int getRowCount(Xt_role xt_role) throws Exception {
		Tool.NullException(xt_role, "xt_role 不能为空");
		return role_queryDAO.selectRowCount(xt_role);
	}

	@Override
	public Xt_role getRoleByID(String role_code) throws Exception {
		Tool.NullException(role_code, "role_code 不能为空");
		return xt_roleDAO.getRoleByID(role_code);
	}

	@Override
	public List<Xt_rolmen> getRolemenByRoleCode(String role_code)
			throws Exception {
		Tool.NullException(role_code, "role_code 不能为空");
		return xt_rolmenDAO.getRolemenByRoleCode(role_code);
	}
	@Override
	public List<Xt_men> getAllMenu()throws Exception{
		List<Xt_men> list = xt_menDAO.getAllMenu();
		if(list==null){
			list = new ArrayList<Xt_men>(0);
		}
		return list;
	}
	/**
	 * 删除后再新增
	 */
	@Override
	public void saveRolemen(String role_code, String[] men_sns)
			throws Exception {
		Tool.NullException(role_code, "role_code 不能为空");
		Tool.noNullElements(men_sns, "men_sns 不能为null,里面的元素也不能为null");

		xt_rolmenDAO.deleteByRoleCode(role_code);
		ArrayList<Xt_rolmen> list = new ArrayList<Xt_rolmen>();
		for (int i = 0; i < men_sns.length; i++) {
			Xt_rolmen rolmen = new Xt_rolmen();
			int key = Key.getKey(KeyConstant.XT_KEY_SN);
			rolmen.setMenrole_sn(String.valueOf(key));
			rolmen.setMen_sn(men_sns[i]);
			rolmen.setRole_code(role_code);
			list.add(rolmen);
		}
		insertSupport.batchInsert(
				"monster.flight.comm.entity.dao.Xt_rolmenDAO.batchInsert", list);

	}

	@Override
	public boolean checkRoleCode(String role_code) throws Exception {
		Tool.NullException(role_code, "role_code 不能为空");
		return xt_roleDAO.checkRoleCode(role_code) > 0;
	}

	@Override
	public List<Role_Meun_TreeVO> getRole_menu_tree(Role_Meun_TreeVO tree_po)
			throws InvalidParameterException, Exception {
		Tool.NullException(tree_po, "Role_Meun_TreeVO 不能为空");
		Tool.NullException(tree_po.getRole_code(), "role_code 不能为空");
		List<Role_Meun_TreeVO> list = role_Menu_Tree_queryDAO.getRole_menu_treeByRole_code(tree_po);
		return list;
	}

}
